﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Northwind.NET.EDM.FE.WinForms.Controls;
using System.Data.Objects;
using Northwind.NET.Model;

namespace Northwind.NET.EDM.FE.WinForms
{
    public class BaseApplicationForm: Form
    {
        protected string _formCaption;
        protected string _userControlName;
        protected string _entitySetName;
        public void Init(string formCaption, string userControlName, string entitySetName)
        {
            _formCaption = formCaption;
            _userControlName = userControlName;
            _entitySetName = entitySetName; 
        }
        public void Init(string userControlName)
        {
            _userControlName = userControlName;
        }

        private BaseUserControl userControl;
        protected void loadControl()
        {
            loadControl(null);
        }

        private UserControl _loadedControl;
        protected void loadControl(Panel controlPanel)
        {

            userControl = ControlsFactory.CreateControlInstance(_userControlName) as BaseUserControl;
            _formCaption = userControl.FormCaption;

            this.Text = _formCaption;

            if (userControl != null && userControl is BaseUserControl)
            {
                ((BaseUserControl)userControl).SetBindingSource();  
            }

            this.SuspendLayout();

            userControl.Location = new System.Drawing.Point(13, 13);
            userControl.Name = string.Format("{0}1", _userControlName.Substring(0,1).ToLower() + _userControlName.Substring(1));
            //userControl.Size = new System.Drawing.Size(500, 281);
            userControl.TabIndex = 0;

            if (controlPanel == null)
            {
                this.ClientSize = new System.Drawing.Size(userControl.Width + 20, userControl.Height + 20);
                this.Controls.Add(userControl);
            }
            else
            {
                _loadedControl = userControl; 
                controlPanel.Controls.Add(userControl);

                //if (string.Compare(_userControlName, "ReportViewingUserControl", true) == 0)
                {
                    //userControl.Width = controlPanel.Width - 100;
                    //userControl.Height = controlPanel.Height - 100;

                    userControl.Dock = DockStyle.Fill;   
                }
            }

            this.ResumeLayout(false);
        }

        //protected void openChildForm(string childFormCaption, string childFormControlName, string childFormEntitySetName)
        //{
        //    UserControlManagerForm managerForm = new UserControlManagerForm();
        //    managerForm.Init(childFormCaption, childFormControlName, childFormEntitySetName);
        //    managerForm.Show();
        //}
        //protected void openChildForm(string childFormControlName)
        //{
        //    UserControlManagerForm managerForm = new UserControlManagerForm();
        //    managerForm.Init(childFormControlName);
        //    managerForm.Show();
        //}

        protected Panel _mainPanel;
        protected void openChildFormInThisForm(Form thisForm, string childFormControlName)
        {
            //UserControlManagerForm managerForm = new UserControlManagerForm();
            //managerForm.Init(childFormControlName);
            //managerForm.Show();

            Init(childFormControlName);

            if (_loadedControl != null)
            {
                _mainPanel.Controls.Remove(_loadedControl);
            }

            loadControl(_mainPanel); 
        }

        protected void todo()
        {
            informationMessage("TO DO!");
        }

        protected void informationMessage(string message)
        {
            MessageBox.Show(message, Northwind.NET.Settings.Globals.APPLICATION_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }
}
